let vm = new Vue({
    el:'#app',  //选中整个main
    data:{
        article:{},//初始化对象     vue建议声明对象同时进行初始化，避免undefinded
        favoriteCount:'',
        favorite:{
            uId:'',
            aId:''
        },
        favoriteNum:'',
        mulFavoriteUsers:{
        },     //共同收藏的好友
        hasMulFavoriteFriends:false,
    },
    methods:{
        //获取收藏数
        getFavoriteCount:function (aId) {
            axios.get(`article/getFavoriteCount/${aId}`).then(result=>{
                this.favoriteCount=result.data.obj;
            }).catch(error=>{

            });
        },
        //增加浏览数
        addBrowseCount:function (aId,browseCount) {
            axios.post(`article/addBrowseCount/${aId}/${browseCount}`).then(result=>{
                this.article=result.data.obj;
            }).catch(error=>{

            });
        },
        //查看收藏情况
        isFavorite:function (aId) {
            this.favorite.aId=aId;
            let uId = JSON.parse(sessionStorage.getItem("user")).id;
            this.favorite.uId=uId;
            axios({
                    url:`article/isFavorite/${this.favorite.uId}/${this.favorite.aId}`,
                }).then(result=>{
                this.favoriteNum=result.data.obj;
                console.log(this.favoriteNum);
            }).catch(error=>{

            });
        },
        //收藏
        toFavorite:function (aId) {
            this.favorite.aId=aId;
            console.log(this.favorite)
            axios.post(
               'article/toFavorite',
                this.favorite
            ).then(result=>{
                layer.msg(result.data.msg);
                //标志已收藏
                this.favoriteNum=1;
                this.favoriteCount++;
                this.selectMulFavoriteUsers(this.article.id);
            }).catch(error=>{
                layer.msg(error.message);
            });
        },
        //取消收藏
        cancelFavorite:function (aId) {
            this.favorite.aId=aId;
            console.log(this.favorite)
            axios.post(
                'article/cancelFavorite',
                this.favorite
            ).then(result=>{
                layer.msg(result.data.msg);
                //标志未收藏
                this.favoriteNum=0;
                this.favoriteCount--;
                this.mulFavoriteUsers={};
                this.hasMulFavoriteFriends=false;
            }).catch(error=>{
                layer.msg(error.message);
            });
        },
        selectMulFavoriteUsers:function (aId) {
            this.favorite.aId=aId;
            axios({
                url:`user/selectMulFavoriteUsers/${this.favorite.uId}/${this.favorite.aId}`,
            }).then(result=>{
                this.mulFavoriteUsers=result.data.obj;
                if (this.mulFavoriteUsers.length>0){
                    this.hasMulFavoriteFriends=true;
                }
            }).catch(error=>{

            });
        },
        //共同收藏的好友的信息
        todetil:function (user) {
            layer.obj = user;
            layer.open({
                type: 2,
                title: '用户详情',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area : ['80%' , '80%'],
                content: 'html/user_detail.html',
            });
        },
    },
    created:function () {
        this.article = parent.layer.obj;
        this.addBrowseCount(this.article.id,this.article.browseCount);
        this.getFavoriteCount(this.article.id);
        this.isFavorite(this.article.id);
        this.selectMulFavoriteUsers(this.article.id);
        console.log(this.favoriteNum)
    }
});